package priv.lirenhe.dissertation.component.search_engine.util;

import java.io.File;

public class ProjectPath {

	/**
	 * 往上迭代（不能往下迭代）得到指定folderNameInProject的绝对路径
	 * 由于目录建立在项目中，如果项目重构，目录也会重构
	 * @param object
	 * @param folderNameInProject
	 * @return
	 */
	public static String getFolderPathInProject(String folderNameInProject){
		//获取项目路径（可单独作为一个类）
		Object object = new Object();
		String filepath = object.getClass().getResource("/").getPath();
		if(filepath != null){
			if(filepath.startsWith("/")){
				filepath = filepath.substring(1, filepath.length());
			}
			File file = new File(filepath);
			if(folderNameInProject != null){
				while(!file.getName().equals(folderNameInProject)){
					filepath = file.getParent();
					if(filepath != null){
						file = new File(filepath);
					}else{
						break;
					}
				}
				if(filepath == null){
					filepath = object.getClass().getResource("/").getPath();
					if(filepath.startsWith("/")){
						filepath = filepath.substring(1, filepath.length());
					}
					if(!filepath.endsWith("/")){
						filepath = filepath + "/" + folderNameInProject +"/";
					}else{
						filepath = filepath + folderNameInProject + "/";
					}
				}
			}
		}
		return filepath;
	}
	
	public static void main(String[] args) {
		
		System.out.println(ProjectPath.getFolderPathInProject("bdb"));
		System.out.println(File.separator);
	}
}
